package cn.wizzer.app.web.modules.controllers.platform.bus.onlyoffice;

import cn.wizzer.app.bus.modules.models.BusinessFileInfo;
import cn.wizzer.app.bus.modules.services.BusinessFileInfoService;
import cn.wizzer.app.onlyoffice.modules.config.OnlyOfficeProperties;
import cn.wizzer.app.onlyoffice.modules.models.documentServer.Action;
import cn.wizzer.app.onlyoffice.modules.models.documentServer.Config;
import cn.wizzer.app.onlyoffice.modules.models.documentServer.DefaultFileWrapper;
import cn.wizzer.app.onlyoffice.modules.models.documentServer.EditorConfig;
import cn.wizzer.app.onlyoffice.modules.services.FileConfigurer;
import cn.wizzer.app.onlyoffice.modules.utils.MD5Util;
import cn.wizzer.app.web.commons.utils.StringUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.util.Assert;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;


@IocBean
@At("/platform/bus/onlyoffice/")
public class IndexController {

    @Inject
    private MD5Util md5Util;
    @Inject
    private OnlyOfficeProperties onlyOfficeProperties;
    @Inject
    private FileConfigurer<DefaultFileWrapper> fileConfigurer;
    @Inject
    private BusinessFileInfoService businessFileInfoService;
    /**
     * 编辑页面
     */
    @At("/edit/?")
    @Ok("beetl:/platform/onlyoffice/edit.html")
    @RequiresAuthentication
    public void edit2(String fileName,@Param(value = "type", df = "desktop") String type,
                        @Param(value = "mode",df = "view") String mode,HttpServletRequest req) {
        try{
            BusinessFileInfo files = businessFileInfoService.fetch(Cnd.where("delFlag", "=", "0").and("fileName", "=", fileName));

            Assert.isTrue(!Objects.isNull(files), "file is not fond!");
            Action action = mode.equals("view") ? Action.view : Action.edit;

            Config config = fileConfigurer.getFileModel(DefaultFileWrapper.builder()
                    .fileName(files.getFileBeforeName())
                    .businessFileInfo(files)
                    .action(action)
                    .user(getUser())
                    .type(type)
                    .build());
            ObjectMapper obj = new ObjectMapper();
            obj.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            obj.writeValueAsString(config);
            req.setAttribute("config", obj.writeValueAsString(config));
            req.setAttribute("api", onlyOfficeProperties.getApi());
        }catch (Exception e){
            e.printStackTrace();
        }
    }




    /**
     * 根据当前登录人设置登录人、名称、分组      本系统只接入编辑与协同
     */
    private EditorConfig.User getUser() {
        EditorConfig.User user = new EditorConfig.User();
        user.setFavorite(true);
        user.setId(StringUtil.getPlatformUid());
        user.setName(StringUtil.getPlatformUser().getUsername());
        user.setGroup("default");
        return user;
    }


}
